import 'dart:convert';

import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:magatron/utils/utils.dart';
import 'package:magatron/view/textfield.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../const.dart';

class Login extends StatefulWidget {
  final String pageIndex;

  Login({String pageIndex}) : this.pageIndex = pageIndex;

  @override
  _LoginState createState() => _LoginState(pageIndex: pageIndex);
}

class _LoginState extends State<Login> {
  String pageIndex;
  Map data = Map<String, String>();
  var _scaffoldkey = new GlobalKey<ScaffoldState>();
  SharedPreferences sp;

  _LoginState({String pageIndex}) : this.pageIndex = pageIndex;

  @override
  void initState() {}

  @override
  void dispose() {
    super.dispose();
    // MyDio.getInstance().clearInstance();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      key: _scaffoldkey,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.fromLTRB(33, 30, 33, 0),
            child: Column(
              children: <Widget>[
//                TextField(
//                  onChanged: (value) {
//                    data['loginName'] = value;
//                  },
//                  style: TextStyle(fontSize: 18, color: Colors.white),
//                  decoration: InputDecoration(
//                    labelText: '用户名',
//                    labelStyle: TextStyle(color: grey, fontSize: 18),
//                    focusedBorder: UnderlineInputBorder(
//                        borderSide: BorderSide(
//                          color: Colors.white,
//                          width: 1,
//                        )),
//                    enabledBorder: UnderlineInputBorder(
//                        borderSide: BorderSide(
//                          color: Colors.white,
//                          width: 1,
//                        )),
//                  ),
//                ),
                EditText(
                  keyboardType: TextInputType.number,
                  data: data,
                  name: 'loginName',
                  labelText: '请输入手机号',
                  value: '',
                ),
                PassField(
                  labelText: '密码',
                  map: data,
                  mapKey: 'passwd',
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[
                    GestureDetector(
                      onTap: () {
                        // String route = '${Routes.forget_pwd}';
                        // Routes.router.navigateTo(context, route,
                        //     replace: false, transition: TransitionType.fadeIn);
                      },
                      child: Text(
                        '忘记密码',
                        style: TextStyle(fontSize: 16, color: gold),
                      ),
                    )
                  ],
                ),
              ],
            ),
          ),
          MaterialButton(
            height: 68,
            minWidth: MediaQuery.of(context).size.width,
            elevation: 0,
            onPressed: () {
              if (data['loginName'] == null) {
                Utils.getInstance().showSnackBar(_scaffoldkey, '用户名不能为空');
                return;
              }
              if (data['passwd'] == null) {
                Utils.getInstance().showSnackBar(_scaffoldkey, '密码不能为空');
                return;
              }
              /* MyDio.getInstance().clearInstance();
              // 登录
              MyDio.getInstance().post(Apis.login, (data) async {
                Utils.getInstance().showSnackBar(_scaffoldkey, data['msg']);

                if (data['resultCode'] == 0) {
                  Utils.getInstance().showSnackBar(_scaffoldkey, data['msg']);
                  LoginBean loginBean = LoginBean.fromJson(data['record']);
                  MyDio.getInstance().get(Apis.memberInfo + loginBean.recordBh, (dataUser) async {
                    print('memberInfo=====${data}');
                    if (dataUser['resultCode'] == 0) {
                      UserInfoBean _userInfoBean = UserInfoBean.fromJson(dataUser['record']);
                      sp = await SharedPreferences.getInstance();
                      sp.clear();
                      sp.setString(PERSON_NUM, _userInfoBean.personNum);
                      sp.setString(UUID, _userInfoBean.recordBh);
                      sp.setString('loginName', _userInfoBean.loginName);
                      sp.setString('nickName', _userInfoBean.nickName);
                      sp.setString('photo', _userInfoBean.photo);
                      sp.setString(AREA_CODE, _userInfoBean.areacode);
                      eventBus.fire(EventBean(title:EVENT_LOGIN));
                      Navigator.pop(context);
                    }else{
                      Utils.getInstance().showSnackBar(_scaffoldkey, data['msg']);
                    }
                  });
                }
              }, params: data);*/
            },
            child: Text(
              "登    录",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
            color: gold,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(30),
                    bottomRight: Radius.circular(30))),
          ),
        ],
      ),
    );
  }

  String _md5Digest(String data) {
    var content = new Utf8Encoder().convert(data);
    var digest = md5.convert(content);
    return hex.encode(digest.bytes);
  }
}
